package com.ikun.admin.modules.gen.queryhandler;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ikun.admin.modules.gen.entity.CodeConfig;
import com.ikun.admin.modules.gen.service.CodeConfigService;
import com.ikun.common.search.customhandler.BaseQueryCustomHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @ClassName TableParamConfigNameCustomHandler
 * @Description:
 * @CreateDate: 2024/4/17 10:21
 */
@Component
public class TableParamConfigNameCustomHandler implements BaseQueryCustomHandler {
    @Autowired
    private CodeConfigService codeConfigService;

    /**
     * 子查询
     * @param colName  从表数据库字段
     * @param value  数据具体值
     * @param queryWrapper 从表 wrapper
     * @return
     */
    @Override
    public Boolean handler(String colName, Object value, QueryWrapper queryWrapper) {
        if (Objects.nonNull(value) && (value instanceof String)) {
            String val = (String) value;
            List<CodeConfig> codeConfigList = codeConfigService.list(new QueryWrapper<CodeConfig>().lambda().
                    like(CodeConfig::getConfigName, val));
            if (codeConfigList.size() == 0) {
                return false;
            } else {
                queryWrapper.in(colName, codeConfigList.stream().map(CodeConfig::getId).collect(Collectors.toSet()));
            }

        }
        return true;
    }
}
